﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace TemplateMatch
{
    public partial class dlgSaveTemplate : Form
    {
        private List<Template> Templates;
        public string folderToSave;
        private int curIndex;
        public dlgSaveTemplate(List<Template> templates)
        {
            InitializeComponent();
            this.Templates = templates;
        }

        private void dlgSaveTemplate_Load(object sender, EventArgs e)
        {
            if (Templates.Count == 0)
            {
                imageBox1.Text = "No template!";
                btnPrevious.Enabled = false;
                btnSave.Enabled = false;
                btnSkip.Enabled = false;
                btnNext.Enabled = false;
                txtName.ReadOnly = true;
            }
            else 
            {
                curIndex = 0;
                imageBox1.Image = Templates[curIndex].GetImage();
                imageBox1.Text = Templates[curIndex].Status.ToString();
                txtName.Text = Templates[0].Name;
                btnPrevious.Enabled = false;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            curIndex++;
            DisplayTemplate();
        }

        private bool SaveCurrentTemplate()
        {
            if (txtName.Text == string.Empty)
            {
                MessageBox.Show("Please give a name to current template!");
                return false;
            }

            try
            {
                if (folderToSave == null || !Directory.Exists(folderToSave))
                {
                    folderBrowserDialog1.Description = "Select folder to save template";
                    folderBrowserDialog1.SelectedPath = Directory.GetCurrentDirectory();
                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                    {
                        folderToSave = folderBrowserDialog1.SelectedPath;
                    }
                }

                Templates[curIndex].Name = txtName.Text;
                Templates[curIndex].Save(folderToSave);
                Templates[curIndex].Status = TemplateStatus.Saved;
                
                return true;
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message);
                Templates[curIndex].Status = TemplateStatus.NotSaved;
                return false;
            }

        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            curIndex--;
            DisplayTemplate();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveCurrentTemplate() == false)
                return;
            if (btnNext.Enabled == true)
            {
                curIndex++;
                DisplayTemplate();
            }
        }

        private void DisplayTemplate()
        {
            imageBox1.Image = Templates[curIndex].GetImage();
            imageBox1.Text = Templates[curIndex].Status.ToString();
            txtName.Text = Templates[curIndex].Name;
            if (curIndex > 0)
            {
                btnPrevious.Enabled = true;
            }
            else
            {
                btnPrevious.Enabled = false;
            }

            if (curIndex == Templates.Count - 1)
            {
                btnNext.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
            }
        }

        private void btnSkip_Click(object sender, EventArgs e)
        {
            Templates[curIndex].Status = TemplateStatus.Skipped;
            imageBox1.Text = Templates[curIndex].Status.ToString();
            if (btnNext.Enabled == true)
            {
                curIndex++;
                DisplayTemplate();
            }
        }

        private void dlgSaveTemplate_FormClosing(object sender, FormClosingEventArgs e)
        {
            int iSaved = Templates.Where(x => x.Status == TemplateStatus.Saved).Count();
            int iSkipped = Templates.Where(x => x.Status == TemplateStatus.Skipped).Count();
            int iNotSaved = Templates.Where(x => x.Status == TemplateStatus.NotSaved).Count();

            string prompt = string.Format("{0} templates saved, {1} templates skipped and {2} templates not saved. Are you sure to quit?", iSaved.ToString(), iSkipped.ToString(), iNotSaved.ToString());

            if (MessageBox.Show(prompt, "Save Templates", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                string f = folderToSave + "\\list.txt";
                StreamWriter sw = new StreamWriter(f);
                foreach(Template t in Templates.Where(x => x.Status == TemplateStatus.Saved).ToList())
                {
                    string o = folderToSave + "\\" + t.Name + ".png" + "\t" + t.Name;
                    sw.WriteLine(o);
                }
                sw.Close();
            }
        }
    }
}
